﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace DNA.Mvc.UI
{
     [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
    public class ReturnAttribute : ActionFilterAttribute
    {
         public override void OnActionExecuted(ActionExecutedContext filterContext)
         {
             var returnUrl = "";
             if (filterContext.RequestContext.RouteData.Values.ContainsKey("returnUrl"))
                 returnUrl = (string)filterContext.RequestContext.RouteData.Values["returnUrl"];

             if (string.IsNullOrEmpty(returnUrl) && filterContext.HttpContext.Request.QueryString.AllKeys.Contains("returnUrl", StringComparer.OrdinalIgnoreCase))
                 returnUrl = filterContext.HttpContext.Request.QueryString["returnUrl"];
          
             if (!string.IsNullOrEmpty(returnUrl) && UrlUtility.CreateUrlHelper().IsLocalUrl(returnUrl))
                 filterContext.Result = new RedirectResult(returnUrl);
             else
                 base.OnActionExecuted(filterContext);
         }
    }
}
